package com.zp.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.entity.SysRoleGroup;
import com.zp.system.entity.bo.SysRoleAllBo;
import com.zp.system.entity.bo.SysRoleGroupBo;
import com.zp.system.entity.vo.SysRoleGroupVo;
import com.zp.system.service.ISysRoleGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 自定义服务信息控制器
 * 前端访问路由地址为:/system/group
 *
 * @author zhang peng
 * @date 2022-12-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/group")
public class SysRoleGroupController extends BaseController {

    private final ISysRoleGroupService iSysRoleGroupService;

    /**
     * 查询自定义服务信息列表
     */
    @SaCheckPermission("system:roleGroup:list")
    @GetMapping("/list")
    public TableDataInfo<SysRoleGroupVo> list (SysRoleGroupBo bo, PageQuery pageQuery) {
        return iSysRoleGroupService.queryPageList(bo, pageQuery);
    }


    @GetMapping("/selectList")
    public R<List<SysRoleGroup>> selectList (SysRoleGroupBo bo) {
        return R.ok(iSysRoleGroupService.selectList(bo));
    }

    /**
     * 获取自定义服务信息详细信息
     *
     * @param roleGroupId 主键
     */
    @SaCheckPermission("system:roleGroup:query")
    @GetMapping("/{roleGroupId}")
    public R<SysRoleGroupVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String roleGroupId) {
        return R.ok(iSysRoleGroupService.queryById(roleGroupId));
    }

    /**
     * 新增自定义服务信息
     */
    @SaCheckPermission("system:roleGroup:add")
    @Log(title = "自定义服务", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@RequestBody SysRoleGroupBo bo) {
        return toAjax(iSysRoleGroupService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改自定义服务信息
     */
    @SaCheckPermission("system:roleGroup:edit")
    @Log(title = "自定义服务", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody SysRoleGroupBo bo) {
        return toAjax(iSysRoleGroupService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除自定义服务信息
     *
     * @param roleGroupIds 主键串
     */
    @SaCheckPermission("system:roleGroup:remove")
    @Log(title = "自定义服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleGroupIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] roleGroupIds) {
        return toAjax(iSysRoleGroupService.deleteWithValidByIds(Arrays.asList(roleGroupIds), true) ? 1 : 0);
    }

    /**
     * 查询角色修改、新增页面自定义服务列
     *
     * @param bo 权限类全部参数
     * @return
     */
    @GetMapping("/getNotExistsRoleGroup")
    public R<Map<String, Object>> getNotExistsRoleGroup (SysRoleAllBo bo) {
        return R.ok(iSysRoleGroupService.getNotExistsRoleGroup(bo.getRoleId(), bo.getOrgId()));
    }

    /**
     * 根据userId、orgId获取已选择的自定义服务列表
     *
     * @param bo 权限类全部参数
     * @return
     */
    @GetMapping("selectListChooseRoleGroup")
    public R<List<SysRoleGroup>> selectListChooseRoleGroup (SysRoleAllBo bo) {
        return R.ok(iSysRoleGroupService.selectListChooseRoleGroup(bo.getUserId(), bo.getOrgId()));
    }
}
